class Solution
{
public:
    int alternatingSubarray(vector<int> &nums)
    {
        int n = nums.size();
        int result = 0;
        for (int i = 0; i < n - 1; ++i)
        {
            if (nums[i] != nums[i + 1] - 1)
            {
                continue;
            }
            int len = 2;
            int j = i + 2;
            while (j < n)
            {
                if (nums[j] == nums[j - 2])
                {
                    ++len;
                }
                else
                {
                    break;
                }
                ++j;
            }
            result = max(result, len);
        }
        return result != 0 ? result : -1;
    }
};